package com.minelx.parallel;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

public class Reducer<E, R> {
	private final Function<List<E>, R> reducer;

	private final List<E> result;

	public Reducer(Function<List<E>, R> reducer) {
		this.reducer = reducer;
		result = new LinkedList<>();
	}

	public synchronized void append(E appended) {
		result.add(appended);
	}

	public R reduce() {
		return reducer.apply(result);
	}
}
